package com.alvis.media.service.impl;

import com.alvis.media.configuration.property.SystemConfig;
import com.alvis.media.service.FileUpload;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

@Service
@AllArgsConstructor
public class FileUploadImpl implements FileUpload {
    private final Logger logger = LoggerFactory.getLogger(FileUpload.class);
    private final SystemConfig systemConfig;

    @Override
    public String uploadFile(InputStream inputStream, long size, String extName) {
        try {
            // 生成唯一文件名
            String fileName = UUID.randomUUID().toString().replaceAll("-", "") + extName;
            
            // 设置文件存储路径（可以根据需要修改）
            String uploadDir = System.getProperty("user.dir") + "/uploads/";
            File dir = new File(uploadDir);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            
            // 保存文件
            Path filePath = Paths.get(uploadDir + fileName);
            Files.copy(inputStream, filePath);
            
            // 返回文件访问路径
            return "/uploads/" + fileName;
        } catch (Exception ex) {
            logger.error("文件上传失败: ", ex);
            return null;
        }
    }
}